package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.sysetm.model.dto.CourseCategoryDTO;
import com.xuecheng.system.convert.SystemConvert;
import com.xuecheng.system.entity.CourseCategory;
import com.xuecheng.system.entity.ex.CourseCategoryNodes;
import com.xuecheng.system.mapper.CourseCategoryMapper;
import com.xuecheng.system.service.CourseCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public List<CourseCategoryDTO> queryAll() {
        //查询数据
        //优先查询redis中有无数据，没有再走数据库
        List<CourseCategoryNodes> result = new ArrayList<>();
        if (redisTemplate.hasKey("courseNodes001")) {
            List<Object> objectList = redisTemplate.opsForHash().values("courseNodes001");
            for (Object obj : objectList) {
                result.add((CourseCategoryNodes) obj);
            }
        }else{
            CourseCategoryMapper baseMapper = this.getBaseMapper();
            result = baseMapper.queryAll();
        }

        //封装结果

        List<CourseCategoryDTO> dtoList = null;
        if (CollectionUtils.isEmpty(result)) {
            dtoList = new ArrayList<>();
            return dtoList;
        }

        dtoList = SystemConvert.INSTANCE.pos2Dtos(result);
        return dtoList;
    }
}
